home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n4.arc
/
LINEDIT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
986b
|
33 lines
//--- LINEDIT.CPP ------------------------ Listing 4 -----------
// Batch editing for a file, a line at a time -- a more complex
// test of string class.
// by Bruce Eckel. See Listing 1 for copyright information.
//--------------------------------------------------------------
#include "stringc.h"
main(int argc, char * argv[]) {
if(argc < 4) {
fprintf(stderr, "usage: linedit search replace filename\n");
exit(1);
}
FILE* fp = fopen(argv[3], "r");
if(fp == NULL) {
fprintf(stderr, "could not open %s\n", argv[3]);
exit(1);
}
const bsz = 80; char buf[bsz];
for(;;) {
if(!fgets(buf, bsz, fp)) break; // quit on end-of-file
buf[strlen(buf) -1] = '\0'; // erase terminating newline
string line(buf);
for(;;) {
int search = line.search(argv[1]);
if(search == -1) break;
line.erase(search, strlen(argv[1]));
line.insert(search, argv[2]);
}
line.print();
}
fclose(fp);
}